/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaProTableColumns, MaProTableExpose } from "@mineadmin/pro-table";
import type { UseDialogExpose } from "@/hooks/useDialog.ts";

import defaultAvatar from "@/assets/images/defaultAvatar.jpg";
import { ElTag } from "element-plus";
import { useMessage } from "@/hooks/useMessage.ts";
import { ResultCode } from "@/utils/ResultCode.ts";
import hasAuth from "@/utils/permission/hasAuth.ts";
import { deleteByIds } from "$/attractions/inspection/api/inspectionPlans.ts";
import { page as getDevicesPage } from "$/attractions/inspection/api/inspectionDevices.ts";

export default function getTableColumns(
  dialog: UseDialogExpose,
  formRef: any,
  t: any
): MaProTableColumns[] {
  const msg = useMessage();

  // 创建设备列表缓存
  const deviceList = ref<any[]>([]);

  // 获取设备列表
  onMounted(async () => {
    try {
      const response = await getDevicesPage({ page: 1, pageSize: 1000 });
      if (response.code === 200 && response.data && response.data.list) {
        deviceList.value = response.data.list;
      }
    } catch (error) {
      console.error('获取设备列表失败:', error);
    }
  });

  return [
    {
      type: "selection",
      showOverflowTooltip: false,
      label: () => t("crud.selection"),
    },
    { 
      label: "巡检计划名称", 
      prop: "name" 
    },
    {
      label: "巡检周期",
      prop: "cycle",
      cellRender: ({ row }) => {
        return (
          <el-tag type="success">
            {row.cycle === 1 ? "每日" : row.cycle === 2 ? "每周" : "每月"}
          </el-tag>
        );
      },
    },
    { 
      label: "巡检人", 
      prop: "person" 
    },
    {
      label: "巡检设备",
      prop: "devices",
      cellRender: ({ row }) => {
        if (!row.devices || !Array.isArray(row.devices)) {
          return "无设备";
        }
        const deviceNames = row.devices.map((deviceId: number) => {
          const device = deviceList.value.find((item) => item.id === deviceId);
          return device ? device.name : `设备${deviceId}`;
        });
        return deviceNames.join(", ");
      },
    },
    {
      type: "operation",
      label: () => t("crud.operation"),
      operationConfigure: {
        actions: [
          {
            name: "edit",
            icon: "material-symbols:person-edit",
            show: ({ row }) =>
              hasAuth("inspection_plans:inspectionPlans:update"),
            text: () => t("crud.edit"),
            onClick: ({ row }) => {
              dialog.setTitle(t("crud.edit"));
              dialog.open({ formType: "edit", data: row });
            },
          },
          {
            name: "del",
            icon: "mdi:delete",
            text: () => t("crud.delete"),
            show: ({ row }) =>
              hasAuth("inspection_plans:inspectionPlans:delete"),
            onClick: ({ row }, proxy: MaProTableExpose) => {
              msg.delConfirm(t("crud.delDataMessage")).then(async () => {
                const response = await deleteByIds([row.id]);
                if (response.code === ResultCode.SUCCESS) {
                  msg.success(t("crud.delSuccess"));
                  proxy.refresh();
                }
              });
            },
          },
        ],
      },
    },
  ];
}
